package com.marketingexpert.controller.marketingmgnt;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.User;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.WebUtils;

public class GetFansController implements Controller {

	private SnsServiceFactory snsServiceFactory;
	private UserService userService;
	private Logger logger = Logger.getLogger(GetFansController.class);
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("application/x-json;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		
		PrintWriter out = response.getWriter();
		
		String userId = request.getParameter("userId");
	
		String cursorPrama = request.getParameter("cursor");
		
		Assert.notNull(userId);
		
		int cursor = 0;
		
		if(cursorPrama != null) {
			cursor = Integer.parseInt(cursorPrama);
		}
		
		User myUser = getUserService().queryUserByUId(userId);
		
		SnsService snsService = WebUtils.getSnsService(myUser, getSnsServiceFactory());
		
		Map<String,Object>  _map = snsService.getMyFollowers(myUser.getAccessToken(), myUser.getSecret(), userId, cursor);
		
		JSONObject result = JSONObject.fromObject(_map);
		
		out.print(result);
		return null;
		
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	

}
